package com.aha.dataanalyzeandview.entity.base;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;
import java.util.Date;

@Data
@Slf4j
public class ResultEntity<T> {
    /**
     *  code=0  成功
     *  code<0  服务器错误
     *  code>0  客户端错误
     */
    private int code;
    private String msg;
    private Date date;
    private Collection<T> data;
    private Integer count;
    private T t;

    private static final String SUCCESS_MESSAGE = "success";

    public static ResultEntity error(int code, String message) {
        log.error("自定义异常返回值信息：[" + message + "]");
        ResultEntity error = new ResultEntity();
        error.setCode(code);
        error.setMsg(message);
        error.setDate(new Date());
        return error;
    }

    // 执行成功时，自定义提示信息
    public static ResultEntity success(String message) {
        ResultEntity success = new ResultEntity();
        success.setCode(0);
        success.setMsg(message);
        success.setDate(new Date());
        return success;
    }

    public static ResultEntity success() {
        ResultEntity success = success(SUCCESS_MESSAGE);
        return success;
    }

    public static <V> ResultEntity<V> success(Collection<V> data) {
        ResultEntity success = success();
        success.setData(data);
        return success;
    }

    public static <V> ResultEntity<V> success(V t) {
        ResultEntity success = success();
        success.setT(t);
        return success;
    }

    public static <V> ResultEntity<V> success(Collection<V> data, V t) {
        ResultEntity success = success();
        success.setData(data);
        success.setT(t);
        return success;
    }
}
